package com.mall.malllibrary.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import me.yokeyword.fragmentation.SupportFragment

abstract class BaseFragment : SupportFragment() {

    // 根节点的view
    private lateinit var mRootView: View

    abstract fun setLayout(): Any

    // 需要子类去实现的类，用于绑定
    abstract fun onBindView(savedInstanceState: Bundle?, rootview: View)

    fun <T : View> findView(@IdRes viewId: Int): T {
        return mRootView.findViewById(viewId)

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootview: View = when {
            setLayout() is Int -> inflater.inflate(setLayout() as Int, container, false)
            setLayout() is View -> setLayout() as View
            else -> {
                throw ClassCastException("类型错误")
            }
        }
        mRootView = rootview
        onBindView(savedInstanceState, rootview)

        return rootview
    }

}